/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SettingsPanel.java
 *
 * Created on Mar 28, 2011, 12:37:04 AM
 */

package edu.fit.cs.sno.applet;

import java.awt.event.ItemEvent;

import edu.fit.cs.sno.snes.Core;
import edu.fit.cs.sno.snes.cpu.Timing;
import edu.fit.cs.sno.snes.ppu.PPU;
import edu.fit.cs.sno.util.Settings;

/**
 *
 * @author Eric
 */
public class SettingsPanel extends javax.swing.JPanel {

    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/** Creates new form SettingsPanel */
    public SettingsPanel() {
        initComponents();initValues();
    }
    private void initValues() {
        try {
			enableFrameSkip.setSelected(Settings.get(Settings.AUTO_FRAME_SKIP).equals("true")?true:false);
		} catch (NullPointerException e) {
			enableFrameSkip.setSelected(false);
		}
        try {
			limitGameSpeed.setSelected(Settings.get(Settings.CPU_LIMIT_SPEED).equals("true")?true:false);
		} catch (NullPointerException e) {
			limitGameSpeed.setSelected(false);
		}
        try {
			autoSkipFrames.setValue(Settings.getInt(Settings.FRAMES_TO_SKIP));
		} catch (NumberFormatException e) {
			autoSkipFrames.setValue(5);
		}
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        OptionsPanel = new javax.swing.JPanel();
        VideoSettingsPanel = new javax.swing.JPanel();
        frameSkipPanel = new javax.swing.JPanel();
        enableFrameSkip = new javax.swing.JCheckBox();
        autoSkipFrames = new javax.swing.JSpinner();
        jLabel1 = new javax.swing.JLabel();
        AudioSettingsPanel = new javax.swing.JPanel();
        enableSound = new javax.swing.JCheckBox();
        EmulatorSettingsPanel = new javax.swing.JPanel();
        limitGameSpeed = new javax.swing.JCheckBox();
        AcceptButton = new javax.swing.JButton();

        OptionsPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Options"));

        VideoSettingsPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Video Settings"));

        frameSkipPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Frameskip"));

        enableFrameSkip.setText("Enable Automatic Frameskip");
        enableFrameSkip.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                enableFrameSkipItemStateChanged(evt);
            }
        });

        autoSkipFrames.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                autoSkipFramesStateChanged(evt);
            }
        });

        jLabel1.setText("Autoskip Frames");

        javax.swing.GroupLayout frameSkipPanelLayout = new javax.swing.GroupLayout(frameSkipPanel);
        frameSkipPanel.setLayout(frameSkipPanelLayout);
        frameSkipPanelLayout.setHorizontalGroup(
            frameSkipPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(frameSkipPanelLayout.createSequentialGroup()
                .addComponent(enableFrameSkip)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 17, Short.MAX_VALUE)
                .addComponent(autoSkipFrames, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel1)
                .addContainerGap())
        );
        frameSkipPanelLayout.setVerticalGroup(
            frameSkipPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(frameSkipPanelLayout.createSequentialGroup()
                .addGroup(frameSkipPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(enableFrameSkip)
                    .addComponent(autoSkipFrames, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout VideoSettingsPanelLayout = new javax.swing.GroupLayout(VideoSettingsPanel);
        VideoSettingsPanel.setLayout(VideoSettingsPanelLayout);
        VideoSettingsPanelLayout.setHorizontalGroup(
            VideoSettingsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(VideoSettingsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(frameSkipPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(68, Short.MAX_VALUE))
        );
        VideoSettingsPanelLayout.setVerticalGroup(
            VideoSettingsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(VideoSettingsPanelLayout.createSequentialGroup()
                .addComponent(frameSkipPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(154, Short.MAX_VALUE))
        );

        AudioSettingsPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Audio Settings"));

        enableSound.setText("Enable Sound");
        enableSound.setEnabled(false);
        enableSound.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                enableSoundItemStateChanged(evt);
            }
        });
        enableSound.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                enableSoundActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout AudioSettingsPanelLayout = new javax.swing.GroupLayout(AudioSettingsPanel);
        AudioSettingsPanel.setLayout(AudioSettingsPanelLayout);
        AudioSettingsPanelLayout.setHorizontalGroup(
            AudioSettingsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(AudioSettingsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(enableSound)
                .addContainerGap(61, Short.MAX_VALUE))
        );
        AudioSettingsPanelLayout.setVerticalGroup(
            AudioSettingsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(AudioSettingsPanelLayout.createSequentialGroup()
                .addComponent(enableSound)
                .addContainerGap(84, Short.MAX_VALUE))
        );

        EmulatorSettingsPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Emulator Settings"));

        limitGameSpeed.setSelected(true);
        limitGameSpeed.setText("Limit Game Speed");
        limitGameSpeed.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                limitGameSpeedItemStateChanged(evt);
            }
        });

        javax.swing.GroupLayout EmulatorSettingsPanelLayout = new javax.swing.GroupLayout(EmulatorSettingsPanel);
        EmulatorSettingsPanel.setLayout(EmulatorSettingsPanelLayout);
        EmulatorSettingsPanelLayout.setHorizontalGroup(
            EmulatorSettingsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(EmulatorSettingsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(limitGameSpeed)
                .addContainerGap(97, Short.MAX_VALUE))
        );
        EmulatorSettingsPanelLayout.setVerticalGroup(
            EmulatorSettingsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(EmulatorSettingsPanelLayout.createSequentialGroup()
                .addComponent(limitGameSpeed)
                .addContainerGap(84, Short.MAX_VALUE))
        );

        AcceptButton.setText("Ok");
        AcceptButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AcceptButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout OptionsPanelLayout = new javax.swing.GroupLayout(OptionsPanel);
        OptionsPanel.setLayout(OptionsPanelLayout);
        OptionsPanelLayout.setHorizontalGroup(
            OptionsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(OptionsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(OptionsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(OptionsPanelLayout.createSequentialGroup()
                        .addGroup(OptionsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(VideoSettingsPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(OptionsPanelLayout.createSequentialGroup()
                                .addComponent(EmulatorSettingsPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(AudioSettingsPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                        .addContainerGap())
                    .addComponent(AcceptButton, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );
        OptionsPanelLayout.setVerticalGroup(
            OptionsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(OptionsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(VideoSettingsPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(OptionsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(EmulatorSettingsPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(AudioSettingsPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(30, 30, 30)
                .addComponent(AcceptButton))
        );

        EmulatorSettingsPanel.getAccessibleContext().setAccessibleName("Emulator Settings");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(OptionsPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(OptionsPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void enableFrameSkipItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_enableFrameSkipItemStateChanged
        if (evt.getStateChange() == ItemEvent.DESELECTED) {
            Settings.set(Settings.AUTO_FRAME_SKIP, "false");
        } else {
            Settings.set(Settings.AUTO_FRAME_SKIP, "true");
        }
        Timing.autoFrameSkip = Settings.isTrue(Settings.AUTO_FRAME_SKIP);
        if (!Timing.autoFrameSkip)
        	PPU.renderFrames = true;
}//GEN-LAST:event_enableFrameSkipItemStateChanged

    private void autoSkipFramesStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_autoSkipFramesStateChanged
    	Settings.set(Settings.FRAMES_TO_SKIP, String.valueOf(autoSkipFrames.getValue()));
        PPU.skipLimit = Settings.getInt(Settings.FRAMES_TO_SKIP);
}//GEN-LAST:event_autoSkipFramesStateChanged

    private void enableSoundItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_enableSoundItemStateChanged
        if (evt.getStateChange() == ItemEvent.DESELECTED) {
            Settings.set(Settings.MUTE_SOUND, "false");
        } else {
            Settings.set(Settings.MUTE_SOUND, "true");
        }
}//GEN-LAST:event_enableSoundItemStateChanged

    private void enableSoundActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_enableSoundActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_enableSoundActionPerformed

    private void limitGameSpeedItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_limitGameSpeedItemStateChanged
        if (evt.getStateChange() == ItemEvent.DESELECTED) {
            Settings.set(Settings.CPU_LIMIT_SPEED, "false");
        } else {
            Settings.set(Settings.CPU_LIMIT_SPEED, "true");
        }
        Timing.limitSpeed = Settings.isTrue(Settings.CPU_LIMIT_SPEED);
}//GEN-LAST:event_limitGameSpeedItemStateChanged

    private void AcceptButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AcceptButtonActionPerformed
    	Core.pause = false;
        SNOApplet.instance.layout.show(getRootPane().getContentPane(), "Game");
    }//GEN-LAST:event_AcceptButtonActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton AcceptButton;
    private javax.swing.JPanel AudioSettingsPanel;
    private javax.swing.JPanel EmulatorSettingsPanel;
    private javax.swing.JPanel OptionsPanel;
    private javax.swing.JPanel VideoSettingsPanel;
    private javax.swing.JSpinner autoSkipFrames;
    private javax.swing.JCheckBox enableFrameSkip;
    private javax.swing.JCheckBox enableSound;
    private javax.swing.JPanel frameSkipPanel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JCheckBox limitGameSpeed;
    // End of variables declaration//GEN-END:variables

}
